package com.ruoyi.fun.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.fun.domain.EcProductComment;
import com.ruoyi.fun.service.IEcProductCommentService;
import com.ruoyi.fun.service.IEcProductService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商品评价Controller
 * 
 * @author ruoyi
 * @date 2025-08-06
 */
@RestController
@RequestMapping("/fun/comment")
public class EcProductCommentController extends BaseController
{
    @Autowired
    private IEcProductCommentService ecProductCommentService;
    
    @Autowired
    private IEcProductService ecProductService;

    /**
     * 查询商品评价列表
     */
    @DataScope(userAlias = "pc", permission = "fun:comment:list")
    @GetMapping("/list")
    public TableDataInfo list(EcProductComment ecProductComment)
    {
        startPage();
        Long orgId = getOrgId();
        // 103组织拥有不过滤的权限
        if (orgId != null && orgId == 103L) {
            orgId = null;
        }
        ecProductComment.setOrgId(orgId);
        List<EcProductComment> list = ecProductCommentService.selectEcProductCommentList(ecProductComment);
        return getDataTable(list);
    }

    /**
     * 导出商品评价列表
     */
    @DataScope(userAlias = "pc", permission = "fun:comment:export")
    @Log(title = "商品评价", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EcProductComment ecProductComment)
    {
        List<EcProductComment> list = ecProductCommentService.selectEcProductCommentList(ecProductComment);
        ExcelUtil<EcProductComment> util = new ExcelUtil<EcProductComment>(EcProductComment.class);
        util.exportExcel(response, list, "商品评价数据");
    }

    /**
     * 获取商品评价详细信息
     */
    @GetMapping(value = "/{commentId}")
    public AjaxResult getInfo(@PathVariable("commentId") Long commentId)
    {
        Long orgId = getOrgId();
        // 103组织拥有不过滤的权限
        if (orgId != null && orgId == 103L) {
            orgId = null;
        }
        return success(ecProductCommentService.selectEcProductCommentByCommentIdAndOrgId(commentId, orgId));
    }

    /**
     * 新增商品评价
     */
    @Log(title = "商品评价", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EcProductComment ecProductComment)
    {
        ecProductComment.setOrgId(getOrgId());
        int result = ecProductCommentService.insertEcProductComment(ecProductComment);
        
        // 如果评论添加成功，更新商品评论数+1
        if (result > 0 && ecProductComment.getProductId() != null) {
            try {
                ecProductService.updateProductCommentCount(ecProductComment.getProductId(), 1);
            } catch (Exception e) {
                System.out.println("更新商品评论数失败: " + e.getMessage());
            }
        }
        
        return toAjax(result);
    }

    /**
     * 修改商品评价
     */
    @Log(title = "商品评价", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EcProductComment ecProductComment)
    {
        return toAjax(ecProductCommentService.updateEcProductComment(ecProductComment));
    }

    /**
     * 删除商品评价
     */
    @Log(title = "商品评价", businessType = BusinessType.DELETE)
	@DeleteMapping("/{commentIds}")
    public AjaxResult remove(@PathVariable Long[] commentIds)
    {
        return toAjax(ecProductCommentService.deleteEcProductCommentByCommentIds(commentIds));
    }

    /**
     * 根据商品ID查询商品评价列表（APP端使用）
     */
    @GetMapping("/product/{productId}")
    public AjaxResult getCommentsByProductId(@PathVariable("productId") Long productId)
    {
        try {
            EcProductComment ecProductComment = new EcProductComment();
            ecProductComment.setProductId(productId);
            ecProductComment.setShowStatus("1"); // 只查询显示状态为1的评价
            List<EcProductComment> list = ecProductCommentService.selectEcProductCommentList(ecProductComment);
            return AjaxResult.success(list);
        } catch (Exception e) {
            logger.error("根据商品ID查询评价失败：" + e.getMessage(), e);
            return AjaxResult.error("查询商品评价失败：" + e.getMessage());
        }
    }
    /**
     * 根据commentId和组织ID获取详细信息
     */
    @GetMapping(value = "/{commentId}/org/{orgId}")
    public AjaxResult getInfoByOrgId(@PathVariable("commentId") Long commentId, @PathVariable("orgId") Long orgId)
    {
        return success(ecProductCommentService.selectEcProductCommentByCommentIdAndOrgId(commentId, orgId));
    }

    /**
     * 根据组织ID查询列表
     */
    @GetMapping("/org/{orgId}")
    public TableDataInfo listByOrgId(@PathVariable Long orgId)
    {
        startPage();
        EcProductComment ecProductcomment = new EcProductComment();
        List<EcProductComment> list = ecProductCommentService.selectEcProductCommentListByOrgId(ecProductcomment, orgId);
        return getDataTable(list);
    }


}